package com.lh0811.forge.pserv.basic.server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lh0811.forge.dependency.basic_code.IdWorker;
import com.lh0811.forge.dependency.basic_web.exception.CommonException;
import com.lh0811.forge.dependency.basic_web.response.ServerResponse;
import com.lh0811.forge.dependency.security.client.component.SecurityUtil;
import com.lh0811.forge.dependency.security.core.model.entity.SecurityAuthority;
import com.lh0811.forge.pserv.basic.server.repository.dao.CommConfigDao;
import com.lh0811.forge.pserv.basic.server.repository.dao.CommConfigTypeDao;
import com.lh0811.forge.pserv.basic.server.repository.entity.CommConfig;
import com.lh0811.forge.pserv.basic.server.repository.entity.CommConfigType;
import com.lh0811.forge.pserv.basic.server.service.CommConfigService;
import com.lh0811.forge.pserv.basic.api.param.*;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class CommConfigServiceImpl implements CommConfigService {

    @Resource
    private CommConfigTypeDao sysConfigTypeDao;

    @Resource
    private CommConfigDao sysConfigDao;

    @Resource
    private CommConfigTypeDao commConfigTypeDao;

    @Resource
    private CommConfigDao commConfigDao;

    @Override
    public CommConfig getConfigValue(String configType, String configKey) throws Exception {
        if (StringUtils.isAnyBlank(configKey, configType)) {
            throw CommonException.create(ServerResponse.createByError("configKey,configType必传"));
        }
        CommConfigType sysConfigType = sysConfigTypeDao.lambdaQuery().eq(CommConfigType::getConfigTypeKey, configType).one();
        if (sysConfigType == null) {
            throw CommonException.create(ServerResponse.createByError("配置类型不存在"));
        }
        CommConfig sysConfig = sysConfigDao.lambdaQuery().eq(CommConfig::getConfigTypeId, sysConfigType.getId())
                .eq(CommConfig::getConfigKey, configKey)
                .one();
        if (sysConfig == null) {
            throw CommonException.create(ServerResponse.createByError("配置信息不存在"));
        }
        return sysConfig;
    }

    @Override
    public PageInfo<CommConfigType> configTypeQueryPage(CommConfigTypeQueryPageParam param) throws Exception {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<CommConfigType> list = commConfigTypeDao.lambdaQuery()
                .like(StringUtils.isNotBlank(param.getConfigTypeKey()), CommConfigType::getConfigTypeKey, param.getConfigTypeKey())
                .like(StringUtils.isNotBlank(param.getConfigTypeName()), CommConfigType::getConfigTypeName, param.getConfigTypeName())
                .like(ObjectUtils.isNotEmpty(param.getEnableFlag()), CommConfigType::getEnableFlag, param.getEnableFlag())
                .orderByDesc(CommConfigType::getCreateTime)
                .list();
        PageInfo pageInfo = new PageInfo<>(list);
        PageHelper.clearPage();
        return pageInfo;
    }

    @Override
    public List<CommConfigType> configTypeQueryList(CommConfigTypeQueryListParam param) throws Exception {
        return commConfigTypeDao.lambdaQuery()
                .like(StringUtils.isNotBlank(param.getConfigTypeKey()), CommConfigType::getConfigTypeKey, param.getConfigTypeKey())
                .like(StringUtils.isNotBlank(param.getConfigTypeName()), CommConfigType::getConfigTypeName, param.getConfigTypeName())
                .like(ObjectUtils.isNotEmpty(param.getEnableFlag()), CommConfigType::getEnableFlag, param.getEnableFlag())
                .orderByDesc(CommConfigType::getCreateTime)
                .list();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void configTypeCreate(CommConfigTypeCreateParam param) throws Exception {
        SecurityAuthority securityAuthority = SecurityUtil.getSecurityAuthority();
        CommConfigType commConfigType = commConfigTypeDao.lambdaQuery()
                .eq(CommConfigType::getConfigTypeKey, param.getConfigTypeKey())
                .one();
        if (ObjectUtils.isNotEmpty(commConfigType)) {
            throw CommonException.create(ServerResponse.createByError("配置类型已经存在"));
        }
        CommConfigType configType = new CommConfigType();
        configType.setId(IdWorker.nextId()); // 主键
        configType.setConfigTypeKey(param.getConfigTypeKey()); // 配置类型
        configType.setConfigTypeName(param.getConfigTypeName()); // 配置类型名称
        configType.setEnableFlag(param.getEnableFlag()); // 是否可用
        configType.setCreateTime(new Date()); // 创建时间
        configType.setCreateBy(Long.valueOf(securityAuthority.getUserId())); // 创建人
        commConfigTypeDao.save(configType);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void configTypeUpdate(CommConfigTypeUpdateParam param) throws Exception {
        SecurityAuthority securityAuthority = SecurityUtil.getSecurityAuthority();
        CommConfigType commConfigType = commConfigTypeDao.getById(param.getId());
        if (ObjectUtils.isEmpty(commConfigType)) {
            throw CommonException.create(ServerResponse.createByError("配置类型不存在"));
        }
        commConfigTypeDao.lambdaUpdate()
                .set(CommConfigType::getUpdateTime, new Date())
                .set(CommConfigType::getUpdateBy, Long.valueOf(securityAuthority.getUserId()))
                .set(StringUtils.isNotBlank(param.getConfigTypeKey()), CommConfigType::getConfigTypeKey, param.getConfigTypeKey())
                .set(StringUtils.isNotBlank(param.getConfigTypeName()), CommConfigType::getConfigTypeName, param.getConfigTypeName())
                .set(ObjectUtils.isNotEmpty(param.getEnableFlag()), CommConfigType::getEnableFlag, param.getEnableFlag())
                .eq(CommConfigType::getId, commConfigType.getId())
                .update();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void configTypeDelete(Long confTypeId) throws Exception {
        CommConfigType commConfigType = commConfigTypeDao.getById(confTypeId);
        if (ObjectUtils.isEmpty(commConfigType)) {
            throw CommonException.create(ServerResponse.createByError("配置类型不存在"));
        }
        commConfigTypeDao.removeById(confTypeId);
        commConfigDao.lambdaUpdate().eq(CommConfig::getConfigTypeId, confTypeId).remove();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void configCreate(CommConfigCreateParam param) throws Exception {
        SecurityAuthority securityAuthority = SecurityUtil.getSecurityAuthority();
        CommConfig commConfigExit = commConfigDao.lambdaQuery()
                .eq(CommConfig::getConfigTypeId, param.getConfigTypeId())
                .eq(CommConfig::getConfigKey, param.getConfigKey())
                .one();
        if (ObjectUtils.isNotEmpty(commConfigExit)) {
            throw CommonException.create(ServerResponse.createByError(ServerResponse.createByError("配置信息已存在")));
        }
        CommConfig commConfig = new CommConfig();
        commConfig.setId(IdWorker.nextId()); // 配置id
        commConfig.setConfigTypeId(param.getConfigTypeId()); // 配置类型id
        commConfig.setConfigName(param.getConfigName()); // 配置名
        commConfig.setConfigKey(param.getConfigKey()); // 配置键
        commConfig.setConfigValue(param.getConfigValue()); // 配置值
        commConfig.setEnableFlag(param.getEnableFlag()); // 是否生效
        commConfig.setCreateTime(new Date()); // 创建时间
        commConfig.setCreateBy(Long.valueOf(securityAuthority.getUserId())); // 创建人
        commConfigDao.save(commConfig);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void configDelete(Long id) throws Exception {
        CommConfig commConfig = commConfigDao.getById(id);
        if (ObjectUtils.isEmpty(commConfig)) {
            throw CommonException.create(ServerResponse.createByError("配置不存在"));
        }
        commConfigDao.removeById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void configUpdate(CommConfigUpdateParam param) throws Exception {
        SecurityAuthority securityAuthority = SecurityUtil.getSecurityAuthority();

        CommConfig commConfig = commConfigDao.getById(param.getId());
        if (ObjectUtils.isEmpty(commConfig)) {
            throw CommonException.create(ServerResponse.createByError("配置不存在"));
        }

        commConfigDao.lambdaUpdate()
                .set(ObjectUtils.isNotEmpty(param.getConfigTypeId()), CommConfig::getConfigTypeId, param.getConfigTypeId())
                .set(StringUtils.isNotBlank(param.getConfigName()), CommConfig::getConfigName, param.getConfigName())
                .set(StringUtils.isNotBlank(param.getConfigKey()), CommConfig::getConfigKey, param.getConfigKey())
                .set(StringUtils.isNotBlank(param.getConfigValue()), CommConfig::getConfigValue, param.getConfigValue())
                .set(CommConfig::getUpdateTime, new Date())
                .set(CommConfig::getUpdateBy, Long.valueOf(securityAuthority.getUserId()))
                .eq(CommConfig::getId, param.getId())
                .update();
    }

    @Override
    public PageInfo<CommConfig> configQueryPage(CommConfigQueryPageParam param) throws Exception {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<CommConfig> list = commConfigDao.lambdaQuery()
                .eq(ObjectUtils.isNotEmpty(param.getConfigTypeId()), CommConfig::getConfigTypeId, param.getConfigTypeId())
                .like(StringUtils.isNotBlank(param.getConfigName()), CommConfig::getConfigName, param.getConfigName())
                .like(StringUtils.isNotBlank(param.getConfigKey()), CommConfig::getConfigKey, param.getConfigKey())
                .eq(ObjectUtils.isNotEmpty(param.getEnableFlag()), CommConfig::getEnableFlag, param.getEnableFlag())
                .orderByDesc(CommConfig::getCreateTime)
                .list();
        PageInfo pageInfo = new PageInfo<>(list);
        PageHelper.clearPage();
        return pageInfo;
    }

    @Override
    public List<CommConfig> configQueryList(CommConfigQueryListParam param) throws Exception {
        return commConfigDao.lambdaQuery()
                .eq(ObjectUtils.isNotEmpty(param.getConfigTypeId()), CommConfig::getConfigTypeId, param.getConfigTypeId())
                .like(StringUtils.isNotBlank(param.getConfigName()), CommConfig::getConfigName, param.getConfigName())
                .like(StringUtils.isNotBlank(param.getConfigKey()), CommConfig::getConfigKey, param.getConfigKey())
                .eq(ObjectUtils.isNotEmpty(param.getEnableFlag()), CommConfig::getEnableFlag, param.getEnableFlag())
                .orderByDesc(CommConfig::getCreateTime)
                .list();
    }


}
